﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworkETL.SaveBatchedChanges
{
    public class StructuredCommandCache
    {
        public StructuredCommandCache()
        {
            ChildCommands=new List<StructuredCommandCache>();
        }

        public StructuredCommandCache(CommandCache commandCache) : this()
        {
            CommandCache = commandCache;
        }
        public List<StructuredCommandCache> ChildCommands { get; set; }
        public StructuredCommandCache ParentCommand { get; set; }
        public CommandCache CommandCache { get; set;} 

        public bool HasAncestor(CommandCache currentDbCommand)
        {
            var currentparrent = this.ParentCommand;
            while (currentparrent != null)
            {
                if (currentparrent.CommandCache.TableName == currentDbCommand.TableName)
                    return true;

                currentparrent = currentparrent.ParentCommand;
            }
            return false;
        }

        public StructuredCommandCache GetAncestor(CommandCache currentDbCommand)
        {
            var currentparrent = this.ParentCommand;
            while (currentparrent != null)
            {
                if (currentparrent.CommandCache.TableName == currentDbCommand.TableName)
                    return currentparrent;

                currentparrent = currentparrent.ParentCommand;
            }
            return null;
        }

        public override string ToString()
        {
            return CommandCache.ToString();
        }
    }
}
